<?php

namespace app\common\model;

use think\Cache;

/**
 * 提问模型类
 * Class Divination
 * @package app\common\model
 */
class Divination extends Base
{
    /**
     * 提问所有的ids
     */
    const REDIS_KEY_IDS = 'divination_ids_';
    /**
     * 提问详情
     */
    const REDIS_KEY_DETAIL = 'divination_detail_';

    protected $table = 'app_divination';// 定义表名

    /**
     * 分页获取列表
     * @param array $map
     * @return array
     */
    public function getListPage($map = [])
    {
        //获取 用户所有提问的id
        $divination_ids = false;
        if ($this->cache_on >= 1)
        {
            $divination_ids = Cache::get(self::REDIS_KEY_IDS . $map['uid']);
        }
        if ($divination_ids === false)
        {
            try
            {
                $divination_ids = $this
                    ->where(['uid' => (int)$map['uid'], 'is_del' => 0])
                    ->order('id DESC')
                    ->column('id');
            }
            catch (\Exception $e)
            {
                // 错误信息写入日志
                \think\Log::write('获取用户提问ids错误:' . '[' . $map['rid'] . ']', 'error', true);
            }
            //存储所有ids
            if ($this->cache_on >= 1)
            {
                Cache::set(self::REDIS_KEY_IDS . $map['uid'], $divination_ids, $this->cache_time);
            }
        }

        if ($map['page'] == -1)
        {//获取所有
            $result = $divination_ids;
        } else
        {//分页获取
            //进行分页返回数据
            $total = count($divination_ids);//统计有多少个id
            $data_list = getPaginatorData($total, $map['page']);//获取数据分页信息
            $result = array_slice($divination_ids, $data_list['start'], $data_list['per_page']);//获取分页后的id数组
            unset($data_list['start']);
            unset($data_list['end']);
        }

        $data_list['data'] = $this->getDetails($result);

        return $data_list;
    }

    /**
     * 查询单个提问的详情
     * @param array $map
     * @return array|bool|mixed
     */
    public function getDetail($map = [])
    {
        $data = false;

        if ($this->cache_on >= 1)
        {
            $data = Cache::get(self::REDIS_KEY_DETAIL . $map['id']);
        }

        if ($data === false)
        {
            try
            {
                $where['id'] = (int)($map['id']);
                $where['is_del'] = 0;

                $data = $this->field('id,uid,question,planet,sign,house,deg,timezone,create_time,longitude,latitude,is_del')
                    ->where($map)->findOrFail()->toArray();
            }
            catch (\Exception $e)
            {
                $this->error = '数据不存在';
                return false;
            }
        }

        return $data;
    }

    /**
     * 获取所有的详情
     * @param array $all_list
     * @return array|bool|false|mixed|\PDOStatement|string|\think\Model
     */
    public function getDetails($all_list = [])
    {
        $result = [];
        // 获取所有未缓存数据的id 用,拼接成字符串
        $ids = $this->getAllNotCacheIds($all_list, self::REDIS_KEY_DETAIL);
        try
        {
            if (strlen($ids) > 0)
            {
                $where = 'id in(' . $ids . ')';
                $result = $this
                    ->field('id,uid,question,planet,sign,house,deg,timezone,create_time,longitude,latitude,is_del')
                    ->where($where)
                    ->where(['is_del' => 0])
                    ->order('id DESC')
                    ->select();
                $result = json_decode(json_encode($result, true), true);
            }
            // 如果缓存打开，循环放入到缓存里
            if ($this->cache_on >= 1)
            {
                if (!empty($result))
                {
                    // 将未缓存的数据，进行缓存
                    foreach ($result as $v)
                    {
                        Cache::set(self::REDIS_KEY_DETAIL . $v['id'], $v, $this->cache_time);
                    }
                }
                $result = [];
                // 循环获取，因为缓存里已经有了数据
                foreach ($all_list as $v)
                {
                    $result[] = Cache::get(self::REDIS_KEY_DETAIL . $v);
                }
            }
        }
        catch (\Exception $e)
        {
            // 错误信息写入日志
            \think\Log::write('获取提问详情错误:' . '[' . $ids . ']', 'error', true);
        }

        return $result;
    }
}
